import { userlist,usercount } from "@/request/api.js"
export default {
    state: {
        userList: [], //列表数据
        page:1,  //当前页码
        size:2,  //一页多少条
        count:0  //总条数
    },
    getters: {
        get_userList(state) {
            return state.userList;
        },
        get_count(state){
            return state.count
        }
    },
    mutations: {
        mutation_userList(state,payload){
            state.userList = payload;
        },
        mutation_count(state,payload){
            state.count = payload;
        },
        mutation_page(state,payload){
            state.page = payload;
        },
    },
    actions: {
        async action_userList({commit,state,dispatch}){
            try{
               let res = await userlist({page:state.page,size:2});
               if(res.code == 200){
                //如果res.list==null,说明这一页没有数据，并且不是第一页，就去获取上一页数据
                if(res.list == null && state.page !=1){
                    //page-1
                    commit("mutation_page",state.page-1)
                    //重新获取前一个页面的数据  掉自己
                    dispatch("action_userList");
                    return;
                }
                 //请求到数据，修改
                 commit("mutation_userList",res.list);
               }
            }catch(err){
                console.log(err);
            }
        },
        //请求总条数
        async action_count({commit}){
            try{
               let res = await usercount();
               if(res.code == 200){
                 //请求到数据，修改
                 commit("mutation_count",res.list[0].total);
               }
            }catch(err){
                console.log(err);
            }
        }
    },
    namespaced: true
}